---
description:
  Yoga uses 4 log levels `debug`, `info`, `warn` and `error`. By default, Yoga will only log `info`,
  `warn` and `error` messages.
---

import { Callout } from '@theguild/components'

# Logging and Debugging

## Logging

Yoga uses 4 log levels `debug`, `info`, `warn` and `error`. By default, Yoga will only log `info`,
`warn` and `error` messages.

### Log Level Overview

#### `error`

- Only log unexpected errors

#### `warn`

- All prior log levels
- deprecation notices

#### `info`

- All prior log levels
- Information about the current state of the system

#### `debug`

- All prior log levels
- Processing of GraphQL parameters
- Parsing of GraphQL parameters
- Execution or subscription start
- Received GraphQL operation variables
- Execution or subscription end
- [GraphiQL](https://github.com/graphql/graphiql) rendering
- Health checks

### Enabling Debug Logging

<Callout>
  Instead, on JavaScript environments that support environment variables via `process.env` (e.g.
  Node.js), you can also conditionally enable `debug` logging by setting the `DEBUG` environment
  variable to `1`.
</Callout>

```ts filename="Custom log level" {7}
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './my-schema.js'

const yoga = createYoga({
  schema,
  logging: 'debug'
})
```

### Change log level

You can customize the log lovel by passing on of the 4 levels to the `logging` option:

<Callout>
  We recommend not changing the default log level and only recommend changing it if you know what
  you are doing.
</Callout>

```ts filename="Custom log level" {7}
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './my-schema.js'

const yoga = createYoga({
  schema,
  logging: 'warn'
})
```

Specifying the log level of `warn` will only log `warn` and `error` messages being logged.

### Custom logger

You can of course provide your own logger for piping to your favorite logging service/utility:

```ts
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { logger } from './my-logger.js'
import { schema } from './my-schema.js'

const yoga = createYoga({
  schema,
  logging: {
    debug(...args) {
      logger.debug(...args)
    },
    info(...args) {
      logger.info(...args)
    },
    warn(...args) {
      logger.warn(...args)
    },
    error(...args) {
      logger.error(...args)
    }
  }
})

const server = createServer(yoga)

server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})
```

Further hook into the logging system by leveraging
[envelop](https://www.envelop.dev/plugins/use-logger)'s
[`useLogger`](https://www.envelop.dev/plugins/use-logger) plugin:

```ts
import { createServer } from 'node:http'
import { createYoga, useLogger } from 'graphql-yoga'
import { logger } from './my-logger'
import { schema } from './my-schema'

const yoga = createYoga({
  schema,
  plugins: [
    useLogger({
      logFn: (eventName, args) => {
        // Event could be execute-start / execute-end / subscribe-start / subscribe-end / etc.
        // args will include the arguments passed to execute/subscribe (in case of "start" event) and additional result in case of "end" event.
        logger.debug(eventName, ...args)
      }
    })
  ]
})

const server = createServer(yoga)

server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})
```
